#include "Qt3DPicker.h"

#include "Move3DHandler.h"
#include "CmdManager.h"
#include "Scene.h"
#include "SendPointHandlerBase.h"
#include "SelectHandler3D.h"
#include "OSGNodeMask.h"
#include "Operation/OpMove.h"
#include "PointHandler.h"
#include "QtOSGWidget.h"

Move3DHandler::Move3DHandler()
{
}

Move3DHandler::~Move3DHandler()
{
}

void Move3DHandler::input(const QVariant& data, const QString& rawInput, CommandSource cs)
{
	auto scene = getScene();
	auto widget3D = getWidget();
	auto sph = getSendPointHandler();
	auto selector = qobject_cast<SelectHandler3D*>(getSelectHandler());
	if (data.type() == QVariant::Bool)
	{
		bool esc = !data.toBool();
		if (esc)
		{
			if (m_clones.size() > 0)
			{
				widget3D->pauseRenderThread();
				for (auto clone : m_clones)
				{
					auto parent = clone->getParent();
					if (parent)
						parent->removeChild(clone);
				}
				widget3D->resumeRenderThread();
			}
			selector->clearSelection();
			clearInput();
			emit finished();
			return;
		}
	}

	if (status() == 0)
	{
		m_pointStateMachine = std::make_shared<PointHandler>();
		m_pointStateMachine->setCmdManager(cmdManager());
		if (selector)
		{
			selector->clearSelection();
			selector->beginSelect(D3CurveMask | D3SurfaceMask);
			prompt(1, tr("选择对象: "));
		}
		else
		{
			emit finished();
		}
		return;
	}

	if (status() == 1)
	{
		if (selector && selector->done())
		{
			auto selections = selector->selectionSet();
			if (selections.size() > 0)
			{
				widget3D->pauseRenderThread();
				for (auto selection : selections)
				{
					std::shared_ptr<Ent> ent = std::dynamic_pointer_cast<Ent>(selection);
					auto parent = ent->getParent();
					if (parent)
					{
						UCS* ucs = dynamic_cast<UCS*>(parent);
						if (ucs)
							continue;
						auto clone = ent->clone();
						m_origins.insert(selection);
						m_clones.push_back(clone);
						parent->addChild(clone);
					}		
				}
				widget3D->resumeRenderThread();

				if (m_origins.size() == 0)
				{
					remark(tr("请选择三维图形"));
					emit finished();
					return;
				}

				if (sph)
				{
					sph->setPointMask(CS_LBTNDOWN | CS_MOUSEMOVE);
				}
				enableMouseHandler(MouseHandlerID::PointSender);
				m_pointStateMachine->setPrompt0(tr("指定起点"));
				m_pointStateMachine->input(data, rawInput, cs);
				setStatus(2);
			}
		}
		return;
	}

	if(status() == 2)
	{
		m_pointStateMachine->input(data, rawInput, cs);
		if (m_pointStateMachine->done())
		{
			m_end = m_start = m_pointStateMachine->getPoint();
			show3DRubberLine(true);
			set3DRubberLineP1(m_start);
			set3DRubberLineP2(m_end);
			m_pointStateMachine->setPrompt0(tr("指定终点"));
			m_pointStateMachine->reset();
			setStatus(3);
		}
		return;
	}

	if (status() == 3)
	{
		if (cs == CS_MOUSEMOVE)
		{
			if (data.type() == QVariant::Vector3D)
			{
				auto pt = data.value<QVector3D>();
				osg::Vec3d point = { pt.x(), pt.y(), pt.z() };
				auto thisoffset = point - m_end;
				m_end = point;
				set3DRubberLineP2(m_end);
				for (auto clone : m_clones)
				{
					clone->move(thisoffset);
				}
			}
		}
		else
		{
			m_pointStateMachine->input(data, rawInput, cs);
			if (m_pointStateMachine->done())
			{
				widget3D->pauseRenderThread();
				for (auto clone : m_clones)
				{
					auto parent = clone->getParent();
					if (parent)
						parent->removeChild(clone);
				}
				widget3D->resumeRenderThread();
				auto pt = m_pointStateMachine->getPoint();
				m_end = pt;
				set3DRubberLineP2(m_end);
				// 记录下本次操作
				const osg::Vec3d offset = m_end - m_start;
				auto selections = selector->selectionSet();
				auto undoStack = scene->undoStack();
				undoStack->push(new OpMove(m_origins,offset));
				selector->clearSelection();
				emit finished();
			}	
		}
	return;
	}
}